package org.flower.web.servlet;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.flower.bean.CommodityClass;
import org.flower.bean.CommodityDetails;
import org.flower.dao.CommodityClassDao;
import org.flower.dao.impl.CommodityClassDaoImpl;
import org.flower.service.CommodityClassService;
import org.flower.service.CommodityDetailsService;
import org.flower.service.impl.CommodityClassServiceImpl;
import org.flower.service.impl.CommodityDetailsServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author Aizen
 */
@WebServlet("/manager/commodityListServlet")
public class CommodityListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CommodityDetailsService commService = new CommodityDetailsServiceImpl();
        CommodityClassService classService = new CommodityClassServiceImpl();

        //价格模糊查询
        String outpriceMins = request.getParameter("outpriceMin");
        String outpriceMaxs = request.getParameter("outpriceMax");

        //session里的模糊查询条件
        String outpriceMin = null;
        String outpriceMax = null;

        //封装模糊查询条件
        CommodityDetails details = new CommodityDetails();
        String comname = request.getParameter("comname");
        details.setComname(comname);


        //获取页码
        String pageNo = request.getParameter("pageNo");
        if(pageNo==null||pageNo.equals("")){
            pageNo="1";
        }

        // 携带参数（最大或者最小都可以）
        if ((outpriceMins!=null&&!outpriceMins.equals("")) || (outpriceMaxs!=null)&&!outpriceMaxs.equals("")) {
            //如果有最小价格则封装进对象
            if(outpriceMins!=null&&!outpriceMins.equals("")){
                request.getSession().setAttribute("outpriceMin",outpriceMins);
                details.setOutPriceMin(Double.parseDouble(outpriceMins));
            }else{
                request.getSession().removeAttribute("outpriceMin");
            }
            //如果有最大价格则封装进对象
            if(outpriceMaxs!=null&&!outpriceMaxs.equals("")){
                request.getSession().setAttribute("outpriceMax",outpriceMaxs);
                details.setOutPriceMax(Double.parseDouble(outpriceMaxs));
            }else{
                request.getSession().removeAttribute("outpriceMax");
            }

        } else if (outpriceMins == null && outpriceMaxs == null && pageNo != null && !pageNo.equals("")) {
            // 没有携带参数 但是携带了分页页码
            outpriceMin = (String) request.getSession().getAttribute("outpriceMin");
            outpriceMax = (String) request.getSession().getAttribute("outpriceMax");
        } else {// 没有携带页码 并且没有携带参数
            request.getSession().removeAttribute("outpriceMin");
            request.getSession().removeAttribute("outpriceMax");
        }


        //查询所有级别下拉框子
        List<CommodityClass> classList = classService.findAll();
        request.setAttribute("classList",classList);

        //查询所有商品
        PageInfo<CommodityDetails> goodsByPage = null;

        String type2classId = (String)request.getSession().getAttribute("type2classId");
        String type1classId = (String)request.getSession().getAttribute("type1classId");


        //如果二级菜单查询不为空 则按二级菜单查询
        if(type2classId!=null&&!type2classId.equals("")){
            details.setClassid(Integer.parseInt(type2classId));
            goodsByPage = commService.findAll(details, Integer.parseInt(pageNo));
            //保存条件

        }else if(type1classId!=null&&!type1classId.equals("")){
            //如果一级菜单不为空 则按一级菜单查询
            goodsByPage = commService.findByPid(Integer.parseInt(pageNo), Integer.parseInt(type1classId));
            //保存条件

        }else{
            //如果没触发菜单查询则默认查询全部
            goodsByPage = commService.findAll(details, Integer.parseInt(pageNo));
        }

        CommodityClassDao classDao = new CommodityClassDaoImpl();
        //处理展示的图片和类别
        List<CommodityDetails> commodityDetailsList = goodsByPage.getList();
        for (CommodityDetails commodityDetails:commodityDetailsList) {
            String[] split = commodityDetails.getComimage().split(",");
            for (String comImage:split) {
                commodityDetails.getComImages().add(comImage);
            }
            String[] split1 = commodityDetails.getDetailsimage().split(",");
            for (String detailsImage:split1) {
                commodityDetails.getDetailsImage().add(detailsImage);
            }
            String className = classDao.getClassNameById(commodityDetails.getClassid());
            commodityDetails.setClassName(className);
        }


        request.setAttribute("goodsByPage",goodsByPage);
        request.getRequestDispatcher("commodityList.jsp").forward(request,response);
    }
}
